package com.example.login.viewmodel


import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.LogUtils

import com.example.common.HttpResListense
import com.example.common.MMKVUtils
import com.example.common.NetlibXK
import com.example.login.api.LoginApi
import com.example.login.model.LoginState
import com.kongzue.dialogx.dialogs.TipDialog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class LGViewModel:ViewModel() {

    private val _loginResult = MutableLiveData<LoginState>()
    val loginResult: LiveData<LoginState> = _loginResult
        fun login(name:String, pw:String){
            LogUtils.d("$name--$pw")
            viewModelScope.launch {
                val ressult = NetlibXK.getInstance().retrofit.create(LoginApi::class.java)
                    .login(name,pw)
               LogUtils.d("res=="+ressult.errorCode)
                if(ressult.errorCode==0){
                    _loginResult.value = LoginState(LoginState._LoginState.LOGINSUCCESS,"")
                }else{
                    TipDialog.show(ressult.errorMsg)
                }
            }
    }

    fun regist(name:String,pw:String){
        viewModelScope.launch {
            val  result = NetlibXK.getInstance().retrofit.create(LoginApi::class.java).register(name,pw,pw)
            if(result.errorCode==0){
                _loginResult.value = LoginState(LoginState._LoginState.REGISTSUCCESS,"")
            }else{
                TipDialog.show(result.errorMsg)
            }
        }
    }

}